#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2011
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
# Author: Jesus Carrero <j.o.carrero@gmail.com>
# Mountain View, CA
#

__docformat__ = 'restructuredtext'

from TimeStepperBase import TimeStepperBase


class TimeStepper(TimeStepperBase):

    """
        m_mat_struct ~ 3, 5 diag or something else.
        m_mfwd ~ matrix to fwd step
        m_mbwd ~ matrix to bwd step
    """

    __slots__ = [
        'm_keep_hist',
        'm_theta',
        'm_mat_struct',
        'm_massat_bound',
        'm_method',
        'm_mfwd',
        'm_mbwd',
        'm_mdiags',
        'm_nstps',
        'm_lfbd',
        'm_log_info',
        'm_ribd',
        'm_stiffat_bound',
        'm_sdiags',
        'm_diag_posi',
        'm_str_time',
        'm_stp_time',
        'm_init_state',
        'm_xl',
        'm_xr',
        ]

    methods = {
        'cn': 'cranck - nicolson',
        'ra': 'rannacher',
        'ee': 'euler explicit',
        'ei': 'euler - implicit',
        'te': 'theta',
        }

    def __init__(self, method):
        TimeStepperBase.__init__(self)

        (self.m_xl, self.m_xr) = (None, None)
        (self.m_str_time, self.m_stp_time, self.m_nstps) = (None, None,
                None)
        (self.m_keep_hist, self.m_log_info) = (False, False)
        (self.m_mfwd, self.m_mbwd) = (None, None)
        self.m_mat_struct = None
        (self.m_mdiags, self.m_sdiags, self.m_diag_posi) = (None, None,
                None)
        (self.m_lfbd, self.m_ribd) = (None, None)

        (self.m_init_state, self.m_massat_bound) = (None, None)
        (self.m_method, self.m_stiffat_bound, self.m_theta) = (None,
                None, None)

        self.set_time_step_method(method)

    def set_time2simulate(self, stp_time, start=0):
        """ set final and start time. """

        (self.m_stp_time, self.m_str_time) = (stp_time, start)

    def set_number_steps(self, nstps):
        """ number of time steps. """

        self.m_nstps = nstps

    def set_time_step_method(self, method):
        """ Implicit, Explicit, Cranck-Nicolson. """

        assert method in self.methods.keys()
        self.m_theta = {'cn' : 0.5,
         'ra' : 0.5, # to be implemented
         'ei' : 0.0,
         'ee' : 1.0, # Never converge :)
         'te' : 1.0,
         }[method]

        self.m_method = method

    def set_xlxr(self, xmin, xmax):
        """ spatial grid boundary. """

        (self.m_xl, self.m_xr) = (xmin, xmax)

    def get_start_time(self):
        """ when to start time stepping. """

        return self.m_str_time

    def get_expiry(self):
        """ stopping time. """

        return self.m_stp_time

    def get_left_right(self):
        """ return boundary condtions at each time step. """

        return (self.m_xl, self.m_xr)

    def set_dolog(self, log_info):
        """ logger class. """

        self.m_log_info = log_info

    def set_keep_hist(self, keep_hist):
        """ keep solution at each time step. """

        self.m_keep_hist = keep_hist

    def get_nsteps(self):
        """ number of steps. """

        return self.m_nstps

    def time_stepper_log(self):
        """ log generated during time stepping. """

        return self.m_log_info

    def keep_history(self):
        """ did we keep the solutions at each time step. """

        return self.m_keep_hist

    def get_methods(self):
        """ Implicit, Eplicit, Cranck-Nicolson. """

        return [self.methods[key] for key in self.methods.keys()]

    def set_mat_attrib(self, struct):
        """ Pentadiagonal, Tridiagonal, ... """

        self.m_mat_struct = struct

    def get_mat_structure(self):
        """ Pentadiagonal, Tridiagonal, ... """

        return self.m_mat_struct

    def get_mass_matrix(self):
        """ mass matrix. """

        return self.m_mdiags

    def get_diag_posi(self):
        """ where to place diagonals in global matrix. """

        return self.m_diag_posi

    def get_stiffness_matrix(self):
        """ stiffness matrix. """

        return self.m_sdiags

    def set_moperators(self, mdatas, sdatas, diags):
        """ matrices in diagoal format. """

        (self.m_mdiags, self.m_sdiags, self.m_diag_posi) = (mdatas,
                sdatas, diags)

    def set_bcond(self, lfbd, ribd):
        """ boundary conditions. """

        (self.m_lfbd, self.m_ribd) = (lfbd, ribd)

    def set_boperators(self, massat_bound, stiffat_bound):
        """ mass and stiffness matrix at the boundary. """

        (self.m_massat_bound, self.m_stiffat_bound) = (massat_bound,
                stiffat_bound)

    def get_mass_stiff_at_bd(self):
        """ mass and stiffness matrix at the boundary. """

        return (self.m_massat_bound, self.m_stiffat_bound)

    def set_ival(self, init_state):
        """ solution at time 0. """

        self.m_init_state = init_state

    def get_initial_state(self):
        """ solution at time 0 """

        return self.m_init_state

    def eval_left_bc(self, xleft, dtime):
        """ vector with bc at each time step. """

        return self.m_lfbd(xleft, dtime)

    def eval_right_bd_cond(self, xleft, dtime):
        """ vector with bc at each time step. """

        return self.m_ribd(xleft, dtime)

    def get_history(self):
        """ virtual method. """

        raise Warning('This is a base class of a virtual method')

    def get_theta(self):
        """ \theat step method. """

        return self.m_theta

    def set_theta(self, theta):
        """ \theat step method. """

        self.m_theta = theta


